﻿#region

using System.Linq;
using System.Web;
using System.Web.Mvc;
using InfoTech.VinhSon.Bussiness;
using InfoTech.VinhSon.Models;
using Telerik.Web.Mvc;

#endregion

namespace VinhSon.Areas.Administrator.Controllers
{
    public class ConfigController : BaseAdministratorController
    {
        //
        // GET: /Administrator/Config/

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GetList()
        {
            return View(new GridModel(UnitOfWork.ConfigRepository.Select()
                                                .Select(c => new
                                                                 {
                                                                         c.Id,
                                                                         c.Name
                                                                 })));
        }

        public ActionResult Edit(string id)
        {
            int guid;
            if(int.TryParse(id,
                            out guid))
            {
                Config t = UnitOfWork.ConfigRepository.SelectbyId(guid);
                if(t != null)
                {
                    ConfigModel configModel = t;
                    return View(configModel);
                }
            }
            return RedirectToAction("Index");
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(ConfigModel confiModel)
        {
            if(ModelState.IsValid)
            {
                Config confi = UnitOfWork.ConfigRepository.SelectbyId(confiModel.Id);
                if(UnitOfWork.ConfigRepository.CheckUpdateConfig(confi))
                {
                    if((confi.IsUploadFile ?? false))
                    {
                        // save document
                        HttpPostedFileBase file = Request.Files[0];
                        if(file != null)
                            if(file.ContentLength > 0)
                            {
                                // delete old resource
                                if(!string.IsNullOrEmpty(confi.Value)
                                   && System.IO.File.Exists(string.Format("{0}{1}",
                                                                          Server.MapPath("~/Resource/Config/"),
                                                                          confi.Value)))
                                    System.IO.File.Delete(string.Format("{0}{1}",
                                                                        Server.MapPath("~/Resource/Config/"),
                                                                        confi.Value));

                                confi.Value = string.Format("{0}_{1}",
                                                            confi.Id,
                                                            file.FileName);
                                file.SaveAs(string.Format("{0}{1}",
                                                          Server.MapPath("~/Resource/Config/"),
                                                          confi.Value));
                            }
                    }
                    else
                        confi.Value = confiModel.Value;

                    UnitOfWork.ConfigRepository.Update(confi);
                    UnitOfWork.Save();
                    return RedirectToAction("Index");
                }
            }
            return View(confiModel);
        }
    }
}
